// pages/personal/about/index.js
const app = getApp();
const { AskPage } = app._require("common/index");
AskPage({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    zIndex: 999999,
    navTab: ["全镇经济", "全镇资源", "各村资源"],
    townName: "",
    eaharts: [],
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTownName();
    this.getData();
    this.getVillAgeData();
    if (this.data.active === 0) {
      wx.setNavigationBarColor({
        frontColor: "#ffffff", // 必写项【该字体颜色仅支持 #ffffff 和 #000000 】
        backgroundColor: "#000000", // 传递的颜色值【仅支持有效值为十六进制颜色】
      });
    }
  },
  onPullDownRefresh: function () {
    this.getTownName();
    this.getData();
    this.getVillAgeData();
  },
  onShow: function () {
    this.getTownName();
    this.getData();
    this.getVillAgeData();
  },
  onChange(event) {
    this.setData({
      active: event.detail.index,
    });
    if (event.detail.index === 0) {
      wx.setNavigationBarColor({
        frontColor: "#ffffff", // 必写项【该字体颜色仅支持 #ffffff 和 #000000 】
        backgroundColor: "#000000", // 传递的颜色值【仅支持有效值为十六进制颜色】
      });
    } else {
      wx.setNavigationBarColor({
        frontColor: "#000000", // 必写项【该字体颜色仅支持 #ffffff 和 #000000 】
        backgroundColor: "#ffffff", // 传递的颜色值【仅支持有效值为十六进制颜色】
      });
    }
  },
  async getTownName() {
    const res = await this.$_server("user/getTownName", {
      params: {
        govId: "?govId=" + app.user.userInfo.governmentId,
      },
    });
    if (res.data.code === 200) {
      this.setData({
        townName: res.data.data.name,
      });
      wx.setNavigationBarTitle({
        title: res.data.data.name + "—" + "资源统计", //页面切换，更换页面标题
      });
    }
  },
  async getData() {
    const response = await this.$_server("statistics/getResourceCount", {
      params: {
        cityCode: app.user.userInfo,
      },
    });
    let { data } = response;
    let arr = data.data.filter((item) => {
      return item.children && item.children.length > 0;
    });
    try {
      this.setData({
        resourceData: arr,
      });
    } catch (e) {}
  },
  async getVillAgeData() {
    const res = await this.$_server("query/village", {
      params: {},
    });
    if (res.data.code === 200) {
      this.setData({
        list: res.data.data.records,
      });
    }
  },
});
